package com.wang.springbootinit.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.BaseResponsePage;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.model.param.SourceFileOperateParam;
import com.wang.springbootinit.model.param.SourceFileQueryParam;
import com.wang.springbootinit.model.vo.SourceFileVO;
import com.wang.springbootinit.service.SourceFileService;
import com.wang.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 源文件操作
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Resource
    private SourceFileService sourceFileService;


    /**
     * 分页查询
     * @param param
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponsePage<List<SourceFileVO>> listPage(@RequestBody SourceFileQueryParam param,HttpServletRequest request){
        log.info("FileController listPage param:{}", JSONUtil.toJsonStr(param));
        return sourceFileService.listPage(param,request);
    }

    /**
     * 删除文件
     * @param param
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean>delete(@RequestBody SourceFileOperateParam param, HttpServletRequest request){
        if(ObjectUtil.isNull(param)){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(sourceFileService.deleteById(param,request));
    }

    /**
     * 文件下载
     * @param param
     * @param request
     * @param response
     */
    @PostMapping("/download")
    public void download(@RequestBody SourceFileOperateParam param, HttpServletRequest request, HttpServletResponse response){
        if(ObjectUtil.isNull(param)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        sourceFileService.download(param,request,response);
    }






}
